package se.kth.ep2300;



import java.util.*;
import java.net.*;
import com.adventnet.snmp.snmp2.*;
import com.adventnet.snmp.snmp2.usm.USMUserEntry;
import com.adventnet.snmp.snmp2.usm.USMUtils;

/**
 * Tutorial from: 
 * http://www.webnms.com/snmp/help/snmpapi/snmpv3/tutorials/overview/lowlevelapi_tutorial.html
 */
class GetTutorial {
	
	public static void main(String args[])
	{
		SnmpAPI api = new SnmpAPI();
		api.setDebug(true);
		
		SnmpSession session = new SnmpSession(api);
		try {
			session.open();
			session.setVersion(3);
		} catch (SnmpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.err.println("FAIL on session - SNMPException!");
			System.exit(-1);
		}
		
		//Build GET Request PDU
		SnmpPDU pdu = new SnmpPDU();
		//UDPProtocolOptions option = new UDPProtocolOptions("192.168.1.10");
		UDPProtocolOptions option = new UDPProtocolOptions("192.168.1.15");
		pdu.setProtocolOptions(option); //sets the host
		
		//pdu.setCommand(SnmpAPI.GETBULK_REQ_MSG);
		//pdu.setMaxRepetitions(1);
		//pdu.setNonRepeaters(0);
 
		//pdu.setCommand(SnmpAPI.GETNEXT_REQ_MSG); 
		pdu.setCommand(SnmpAPI.GET_REQ_MSG); 
		
		// Authentication testing...
		pdu.setUserName("2G1332_student".getBytes());
		try {
			USMUtils.init_v3_parameters("2G1332_student", USMUserEntry.MD5_AUTH, "netmanagement",null , option.getRemoteHost(), option.getRemotePort(), session);
		} catch (SnmpException e) {
			e.printStackTrace();
		}

		
		
		
		//String oidStr = ".1.3.6.1.2.1.2.1";		// ifNumber
		//String oidStr = ".1.3.6.1.2.1.2.2.1.2";		// ifType
		//String oidStr = ".1.3.6.1.2.1.1.5";		// SysName 
		//String oidStr = ".1.3.6.1.2.1.2.2.1.1";	// ifIndex
		//String oidStr = ".1.3.6.1.2.1.4.21.1.7";	// ipRouteNextHop
		String oidStr = ".1.3.6.1.2.1.4.20.1.1.2";	// ifAdEntAddr
		//String oidStr = ".1.3.6.1.2.1.4.20.1.2";	// ifAdEntIndex
		SnmpOID oid1 = new SnmpOID(oidStr);

		//SnmpOID oid = new SnmpOID(".1.3.6.1.2.1.6.12.0");  //Next should be TCPConnTable, but get 6.14 instead
		//SnmpOID oid1 = new SnmpOID(".1.3.6.1.2.1.2.2.1.3.1");  //Next should be MAC addr
		//SnmpOID oid2 = new SnmpOID(".1.3.6.1.2.1.2.2.1.3.2");  //Next should be MAC addr
		//SnmpOID oid3 = new SnmpOID(".1.3.6.1.2.1.2.2.1.3.3");  //Next should be MAC addr
		//SnmpOID oid4 = new SnmpOID(".1.3.6.1.2.1.4.22.1.1.1"); // Next IP Route Info: .1.3.6.1.2.1.4.22.1.1.192.168.1.10 (?)
		//SnmpOID oid1 = new SnmpOID(".1.3.6.1.2.1.4.22.1.1.1");
		//SnmpOID oid2 = new SnmpOID(".1.3.6.1.2.1.4.22.1.1.2");
		//SnmpOID oid3 = new SnmpOID(".1.3.6.1.2.1.4.22.1.2.1");
		//SnmpOID oid4 = new SnmpOID(".1.3.6.1.2.1.4.22.1.2.2");
		//SnmpOID oid1 = new SnmpOID(".1.3.6.1.2.1.4.21.1.2");
		//SnmpOID oid1 = new SnmpOID(".1.3.6.1.2.1.3.1");	// GETBULK example
		//SnmpOID oid1 = new SnmpOID(".1.3.6.1.2.1.4.21.1.1");
		//SnmpOID oid2 = new SnmpOID(".1.3.6.1.2.1.4.21.1.2.192.168.0.0");
		pdu.addNull(oid1);
		//pdu.addNull(oid2);
		//pdu.addNull(oid3);
		//pdu.addNull(oid4);
		//pdu.addNull(oid4);
		//pdu.addNull(oid1);
		//pdu.addNull(oid2);
		//pdu.addNull(oid3);
		
		SnmpPDU result;
		List ips = new ArrayList();

		try {
			result = session.syncSend(pdu);
			System.out.println(result.printVarBinds());
			System.out.println("Value: " + result.getVariableBinding(0).getVariable().toValue());
		
			System.out.println("\n----------------");
			int numRows = 0;
				
				SnmpVarBind svb = result.getVariableBinding(0);
				//System.out.println("OID: " + svb.getObjectID() + ";\t" 
				//	+ svb.getVariable().getType() + ":\t"
				//	+ svb.getVariable().toValue()); 
				while ( svb.getObjectID().toString().startsWith(oidStr) ) {
					numRows++;
					ips.add(svb.getVariable().getVarObject());
						
					SnmpPDU nextPdu = new SnmpPDU();
					nextPdu.setProtocolOptions(option); //sets the host
					nextPdu.setUserName("2G1332_student".getBytes());
				
					nextPdu.setCommand(SnmpAPI.GETNEXT_REQ_MSG); 
					nextPdu.addNull(svb.getObjectID());
					SnmpPDU nextResult;
					result = session.syncSend(nextPdu);
					//System.out.println("ROWS: " + numRows + " --- " + 
					//	((SnmpVarBind) result.getVariableBindings().elementAt(0)).getVariable().getVarObject());
					//ips.add(((SnmpVarBind) result.getVariableBindings().elementAt(0)).getVariable().getVarObject());
					svb = (SnmpVarBind) result.getVariableBinding(0);
				}		
				

			numRows--;
			
			System.out.println("NUM ROWS: " + numRows);
			System.out.println(ips);
			
		} catch (SnmpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.err.println("FAIL on result - SNMPException!");
			System.exit(-1);
		} catch (NullPointerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.err.println("FAIL on result - NULL!");
			System.exit(-1);
		}
		
		session.close();
		api.close();

	}
}
